/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto.gui;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import projeto.erro.GeralException;
import projeto.modelo.fachada.Fachada;
import projeto.modelo.to.Cidade;
import projeto.modelo.to.Endereco;

/**
 *
 * @author Daniel
 */
public class GuiEnderecoNovo extends javax.swing.JDialog {
    
    public static Fachada fachada = new Fachada();

    /**
     * Creates new form GuiEnderecoNovo
     */
    public GuiEnderecoNovo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    GuiEnderecoNovo() {
        initComponents();
        setLocationRelativeTo(null);
        atualizarComboCidade();
        bloquearTela();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GuiEnderecoNovo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GuiEnderecoNovo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GuiEnderecoNovo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GuiEnderecoNovo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                GuiEnderecoNovo dialog = new GuiEnderecoNovo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings({"unchecked", "CallToThreadDumpStack"})
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelNovoEndereco = new javax.swing.JPanel();
        jLabelCEP = new javax.swing.JLabel();
        jLabelLogradouro = new javax.swing.JLabel();
        jLabelCidade = new javax.swing.JLabel();
        jTextFieldEntradaLog = new javax.swing.JTextField();
        jComboBoxCidade = new javax.swing.JComboBox();
        jButtonConsultarCEP = new javax.swing.JButton();
        jButtonSalvar = new javax.swing.JButton();
        jButtonGerenciarCidade = new javax.swing.JButton();
        jFormattedTextFieldCEP = new javax.swing.JFormattedTextField();
        jButtonVoltar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Salvar novo endereço");
        setModal(true);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanelNovoEndereco.setBorder(javax.swing.BorderFactory.createTitledBorder("Novo Endereço"));

        jLabelCEP.setText("CEP.:");

        jLabelLogradouro.setText("Logradouro.:");

        jLabelCidade.setText("Cidade.:");

        jTextFieldEntradaLog.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        jButtonConsultarCEP.setText("Consultar CEP");
        jButtonConsultarCEP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConsultarCEPActionPerformed(evt);
            }
        });

        jButtonSalvar.setText("Salvar Endereço");
        jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarActionPerformed(evt);
            }
        });

        jButtonGerenciarCidade.setText("Gerenciar Cidade");
        jButtonGerenciarCidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGerenciarCidadeActionPerformed(evt);
            }
        });

        try {
            jFormattedTextFieldCEP.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jButtonVoltar.setText("Voltar");
        jButtonVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelNovoEnderecoLayout = new javax.swing.GroupLayout(jPanelNovoEndereco);
        jPanelNovoEndereco.setLayout(jPanelNovoEnderecoLayout);
        jPanelNovoEnderecoLayout.setHorizontalGroup(
            jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelNovoEnderecoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelLogradouro)
                    .addComponent(jLabelCEP)
                    .addComponent(jLabelCidade))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelNovoEnderecoLayout.createSequentialGroup()
                        .addGroup(jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jComboBoxCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jFormattedTextFieldCEP)
                            .addComponent(jButtonVoltar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButtonConsultarCEP, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonGerenciarCidade, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonSalvar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jTextFieldEntradaLog))
                .addContainerGap())
        );
        jPanelNovoEnderecoLayout.setVerticalGroup(
            jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelNovoEnderecoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCEP)
                    .addComponent(jButtonConsultarCEP, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldCEP, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelLogradouro)
                    .addComponent(jTextFieldEntradaLog, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCidade)
                    .addComponent(jComboBoxCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonGerenciarCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelNovoEnderecoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelNovoEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelNovoEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConsultarCEPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConsultarCEPActionPerformed
        consultarCep();
    }//GEN-LAST:event_jButtonConsultarCEPActionPerformed

    private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarActionPerformed
        salvarEndereco();        
    }//GEN-LAST:event_jButtonSalvarActionPerformed

    private void jButtonGerenciarCidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGerenciarCidadeActionPerformed
        gerenciarCidadeEnd();        
    }//GEN-LAST:event_jButtonGerenciarCidadeActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        //
    }//GEN-LAST:event_formComponentShown

    private void jButtonVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVoltarActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonVoltarActionPerformed

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        atualizarComboCidade();
    }//GEN-LAST:event_formWindowGainedFocus
    
    /**
     * Método que salva um novo registro Endereco;
     */
    private void salvarEndereco() {
        int resposta;
        int enderecos_Codigo = 0;
        int cidades_Codigo;
        String enderecos_CEP;
        String enderecos_Logradouro;
        String cidades_Nome;
        String str_cep;
        Endereco end;
        try {
            str_cep = jFormattedTextFieldCEP.getText();
            str_cep = str_cep.replace('-', ' ');
            str_cep = str_cep.replaceAll(" ", "");
            enderecos_CEP = str_cep;
            enderecos_Logradouro = jTextFieldEntradaLog.getText();
            cidades_Nome = jComboBoxCidade.getSelectedItem().toString();
            Cidade cd = fachada.consultarCidade(cidades_Nome);
            cidades_Codigo = cd.getCidades_Codigo();
            end = new Endereco(enderecos_Codigo, enderecos_CEP, enderecos_Logradouro, cidades_Codigo, cidades_Nome);
            fachada.salvarEndereco(end);
            resposta = JOptionPane.showConfirmDialog(null, "Registro salvo com sucesso!\nDeseja cadastrar outro endereço?", "", JOptionPane.YES_NO_OPTION);
            if (resposta == JOptionPane.NO_OPTION) {
                dispose();                
            } else {
                limparCampos();
                bloquearTela();
            }
        } catch (GeralException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    /**
     * Método que verifica se já existe o CEP cadastrado, caso exista, escolha se quer consultar outro ou sair;
     */
    private void consultarCep() {
        int resComCadastro;
        int resSemCadastro;
        String str_cep;
        String enderecos_CEP;
        Endereco end;
        try {
            str_cep = jFormattedTextFieldCEP.getText();
            str_cep = str_cep.replace('-', ' ');
            str_cep = str_cep.replaceAll(" ", "");
            enderecos_CEP = str_cep;
            if((enderecos_CEP == null) || (enderecos_CEP.equals(""))){
                JOptionPane.showMessageDialog(null, "Digite um CEP!");
                bloquearTela();
            } else {
                end = fachada.consultarEndCep(enderecos_CEP);
                if (end != null) {
                    resComCadastro = JOptionPane.showConfirmDialog(null, "CEP já está cadastrado!\nDeseja cadastrar outro?", "", JOptionPane.YES_NO_OPTION);
                    if (resComCadastro == JOptionPane.YES_OPTION) {
                        limparCampos();
                        bloquearTela();
                    } else {
                        dispose();
                    }
                } else {
                    resSemCadastro = JOptionPane.showConfirmDialog(null, "CEP não está cadastrado!\nDeseja continuar?", "", JOptionPane.YES_NO_OPTION);
                    if (resSemCadastro == JOptionPane.YES_OPTION) {
                        liberarTela();
                    } else {
                        dispose();
                    }
                }
            }
        } catch (GeralException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }
    
    private void gerenciarCidadeEnd(){
        String cep = jFormattedTextFieldCEP.getText();
        String log = jTextFieldEntradaLog.getText();
        dispose();
        GuiCidade cidade = new GuiCidade();
        cidade.setVisible(true);
        GuiEnderecoNovo guiEnderecoNovo = new GuiEnderecoNovo();
        GuiEnderecoNovo.jFormattedTextFieldCEP.setText(cep);
        GuiEnderecoNovo.jTextFieldEntradaLog.setText(log);
        liberarTela();
        jComboBoxCidade.requestFocus();
        guiEnderecoNovo.setVisible(true);
    }

    /**
     * Método que cria um ArrayList de Cidade, para ser inserido no ComboBox Cidade;
     */
    private void atualizarComboCidade() {
        Cidade cid;
        ArrayList<Cidade> listaCd;
        try {
            listaCd = (ArrayList<Cidade>) fachada.listarCidadeTudo();
            for (Iterator<Cidade> it = listaCd.iterator(); it.hasNext();) {
                cid = it.next();
                jComboBoxCidade.addItem(cid.getCidades_Nome());
            }
        } catch (GeralException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }
    
    /**
     * Método que limpa os campos dos filtros;
     */
    private void limparCampos(){
        //limpar os edits
        jFormattedTextFieldCEP.setValue(null);
        jTextFieldEntradaLog.setText("");
        jFormattedTextFieldCEP.requestFocus();
    }
    
    public static void liberarTela(){
        jTextFieldEntradaLog.setEnabled(true);
        jComboBoxCidade.setEnabled(true);
        jButtonGerenciarCidade.setEnabled(true);
        jButtonSalvar.setEnabled(true);
        jTextFieldEntradaLog.requestFocus();
    }
    
    public static void bloquearTela(){
        jTextFieldEntradaLog.setEnabled(false);
        jComboBoxCidade.setEnabled(false);
        jButtonGerenciarCidade.setEnabled(false);
        jButtonSalvar.setEnabled(false);
        jFormattedTextFieldCEP.requestFocus();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConsultarCEP;
    private static javax.swing.JButton jButtonGerenciarCidade;
    private static javax.swing.JButton jButtonSalvar;
    private javax.swing.JButton jButtonVoltar;
    public static javax.swing.JComboBox jComboBoxCidade;
    public static javax.swing.JFormattedTextField jFormattedTextFieldCEP;
    private javax.swing.JLabel jLabelCEP;
    private javax.swing.JLabel jLabelCidade;
    private javax.swing.JLabel jLabelLogradouro;
    private javax.swing.JPanel jPanelNovoEndereco;
    public static javax.swing.JTextField jTextFieldEntradaLog;
    // End of variables declaration//GEN-END:variables
}
